package com.solitaire.and;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;

import org.json.JSONObject;


public class SoliMaxReward {
    private static SoliMaxReward soliMaxReward;
    private MaxRewardedAd maxRewardedAd;
    private String adSource="";

    public static SoliMaxReward getInstance() {
        if (soliMaxReward == null) {
            soliMaxReward = new SoliMaxReward();
        }
        return soliMaxReward;
    }

    public void loadRewardAd(Context context) {
        getRewardAD(context);
        maxRewardedAd.loadAd();
    }

    public void showAd(Activity activity, String adSource) {
        if (check()){
            setAdSource(adSource);
            show(activity);
        }
    }

    private boolean check() {
        if (maxRewardedAd==null || !maxRewardedAd.isReady()) {
            callbackAdError();
            maxRewardedAd.loadAd();
            return false;
        }
        return true;
    }

    private void show(Activity activity) {
        getRewardAD(activity);
        maxRewardedAd.showAd(activity);
    }

    private void setAdSource(String adSource) {
        if (adSource!=null){
            this.adSource = adSource;
        }
    }

    private void getRewardAD(Context context) {
        if (maxRewardedAd == null) {
            maxRewardedAd =MaxRewardedAd.getInstance(SoliConstant.REWARD_PLACEMENTID, context);
            maxRewardedAd.setListener(new MaxRewardedAdListener() {
                @Override
                public void onUserRewarded(@NonNull MaxAd maxAd, @NonNull MaxReward maxReward) {

                }

                @Override
                public void onAdLoaded(@NonNull MaxAd maxAd) {

                }

                @Override
                public void onAdDisplayed(@NonNull MaxAd maxAd) {

                }

                @Override
                public void onAdHidden(@NonNull MaxAd maxAd) {
                    maxRewardedAd.loadAd();
                    try {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("ad_source", adSource);
                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                        SoliCocosBridge.getInstance().videoCallback("ad_over_filing", jsonObject.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onAdClicked(@NonNull MaxAd maxAd) {

                }

                @Override
                public void onAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {

                }

                @Override
                public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError maxError) {
                    callbackAdError();
                }
            });
        }
    }

    public void callbackAdError() {
        SoliCocosBridge.getInstance().videoCallback("ad_error_filing", "{}");
    }

}
